import 'package:aoe_flutter/mvvm/bundle/bundle_bus.dart';
import 'package:aoe_flutter/utils/global.dart';
import 'package:flutter/material.dart';
import 'package:package_info/package_info.dart';

import 'mvvm/bundle/bundle.dart';
import 'service/login_control.dart';

class APP {
  String baseUrl;
  static APP _instance;

  Global _global;
  bool _menuFree;
  PackageInfo _packageInfo;
  UserInfo userInfo;
  GlobalKey<NavigatorState> _navigatorKey = GlobalKey();
  Map<String, WidgetBuilder> _routers = {};
  String _appId;


  static Future<APP> get instance async {
    return getInstance();
  }

  APP._();



  Future init({@required String baseUrl, bool menuFree = false}) async {
    _global = await Global.getInstance();
    _packageInfo = await PackageInfo.fromPlatform();
    _menuFree=menuFree;
 
    userInfo = _global.userInfo;
    // if (userInfo != null) {
    //   await _socketClient.connect();
    // }
    _global.init(baseUrl: baseUrl);

  }

  void installBundles(String groupName, List<Bundle> bundles) {
    bundles.forEach((bundle) {
      BundleBus.register(groupName, bundle);
      _routers.putIfAbsent(bundle.id, () => (_) => bundle);
    });
  }

  static APP getInstance() {
    WidgetsFlutterBinding.ensureInitialized();
    if (_instance == null) {
      _instance = new APP._();
    }
    return _instance;
  }

  Global get global => _global;

  PackageInfo get packageInfo => _packageInfo;
  bool get menuFree => _menuFree;
}
